package com.sxt.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerAutoConfig {

	@Autowired
	private SwaggerProperty swaggerPropety;

	@Bean
	public Docket docket() {
		ApiSelectorBuilder apis = new Docket(DocumentationType.SWAGGER_2).
				apiInfo(getApiInfo()).
				select().
				apis(RequestHandlerSelectors.basePackage(swaggerPropety.getPackages()));
		return apis.build();
	}

	private ApiInfo getApiInfo() {
		Contact contact = new Contact(swaggerPropety.getUserName(), swaggerPropety.getUrl(), swaggerPropety.getEmail());
		return new ApiInfoBuilder().
				contact(contact).
				description(swaggerPropety.getDesc()).
				termsOfServiceUrl(swaggerPropety.getTeamOfService()).
				license(swaggerPropety.getLicense()).
				licenseUrl(swaggerPropety.getLicenseUrl()).
				title(swaggerPropety.getTitle()).build();
	}
}
